<?php
namespace app\open\controller;
use think\Db;

class Index extends Base
{
    public function index(){
        $whereMenu = 'status=1';

        $menuList = Db::name('CpMenu')->where($whereMenu)->order('sort desc')->select();
        import('cate.Category');
        $leftMenu = \Category::unlimitedForLayer($menuList, $name='list', 1);

        $isLockScreen = cookie('is_lock_screen') ? true : false;

        //今日数据
        $startTime = strtotime(date('Y-m-d', time()));
        $endTime = time();

        $year = date('Y', $startTime);
        $month = date('Ym', $startTime);

        $gameIdStr = session('admin.game_id');
        $whereAdd = "bond_time between {$startTime} AND {$endTime} && game_id in ({$gameIdStr})";
        $whereActive = "in_time between {$startTime} AND {$endTime} && game_id in ({$gameIdStr})";
        $whereOrder = "pay_time between {$startTime} AND {$endTime} AND debug=0 AND pay_result=1 && game_id in ({$gameIdStr})";

        /*点西游特殊处理*/
        if(session('admin.id')==43){
            $whereAdd .= " && bond_time<1525104000";
            $whereActive .= " && in_time<1525104000";
            $whereOrder .= " && pay_time<1525104000";
        }


        $add = Db::name('GameUserY_'.$year)
            ->field("count(*) AS game_add")
            ->where($whereAdd)
            ->find();

        $active = Db::name('GameUserLogM_'.$month)
            ->field("count(DISTINCT(my_user_id)) AS game_active")
            ->where($whereActive)
            ->find();

        $order = Db::name('PayOrderY_'.$year)
            ->field("count(DISTINCT(my_user_id)) AS pay_number,sum(total_price) AS pay_money")
            ->where($whereOrder)
            ->find();

        $today = ['game_add'=>$add['game_add'], 'game_active'=>$active['game_active'], 'pay_number'=>$order['pay_number'], 'pay_money'=>$order['pay_money']/100];

        $skinName = cookie('skin_name') ? cookie('skin_name') : '';
        return view('', ['today'=>$today, 'login'=>session('admin'), 'skin_name'=>$skinName, 'is_lock_screen'=>$isLockScreen, 'leftMenu'=>$leftMenu]);
    }

    public function quit(){
        session('admin', null);
        $this->redirect('Show/login');
    }

    public function game(){
        $page = input('page');
        $limit = input('limit');

        $gameIdStr = session('admin.game_id');
        $where['status'] = 1;
        $where['game_id'] = ['in', $gameIdStr];
        if( ! $count=Db::name('ViewGameManage')->where($where)->count() ){
            $this->layTable();
        }

        $select = Db::name('ViewGameManage')
            ->where($where)
            ->order('game_id desc')
            ->limit(($page-1)*$limit, $limit)
            ->select();

        $data = [];
        $appClassIdName = changeValKey('AppClass', 'id', 'name');
        $appPublishIdName = changeValKey('AppPublishType', 'id', 'name');
        foreach ($select as $k=>$v){
            $data[] = [
                'game_id' => $v['game_id'],
                'game_name' => $v['game_name'],
                'app_class_id' => isset($appClassIdName[ $v['app_class_id'] ]) ? $appClassIdName[ $v['app_class_id'] ] : '',
                'app_publish_type_id' => isset($appPublishIdName[ $v['app_publish_type_id'] ]) ? $appPublishIdName[ $v['app_publish_type_id'] ] : '',
                'package_size' => round(($v['package_size'])/(1024*1024),2) . 'MB',
                'version_str' => $v['version_str'],
                'status' => $v['status'] ? '已上线' : '暂定中',
            ];
        }

        $this->layTable(0, '游戏列表', $data, $count);
    }
}
